package org.apache.ibatis.demo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.demo.bean.User;

import java.io.IOException;
import java.io.InputStream;

public class ResultMapDemo {

    public static void main(String[] args) throws IOException {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        // 定义资源文件
        InputStream resource = Resources.getResourceAsStream("org\\apache\\ibatis\\test\\conf\\mybatis.xml");
        // 根据配置配置文件构建会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 开启会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user1 = sqlSession.selectOne("select * from users where id  = 1", User.class);

        // 获取Mapper接口
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        // 执行SQL
        User user = userDao.getOnlyReadResult(1L);
        // 打印结果
        System.out.println(user.getOnlyReadField());
    }

}
